package com.webvihar.access2db.activities;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.webvihar.access2db.R;
import com.webvihar.access2db.model.Session;
import com.webvihar.common.db.DbService;

public class DeleteSessionActivity extends BaseActivity implements OnClickListener{
	
	private DbService db;
	private EditText nameTxt;
	private EditText hostTxt;
	private EditText portTxt;
	private EditText usernameTxt;
	private EditText passwordTxt;
	private Button deleteSessionBtn;
	private ImageButton addButton;
	private TextView titleTxt;

	private String SESSION_SESSION_NAME = "SessionName";
	private String SESSION_SESSION_HOST = "SessionHost";
	private String SESSION_SESSION_PORT = "SessionPort";
	private String SESSION_SESSION_USERNAME = "SessionUsername";
	private String SESSION_SESSION_PASSWORD = "SessionPassword";

	private String sessionName;
	private String host;
	private String port;
	private String username;
	private String password;
	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
		setContentView(R.layout.add_session);
		loadBundleData(getIntent().getExtras());
		initUI();
		setTitleBar();
		setListeners();
		initDb();
	}

	private void setTitleBar() {
		titleTxt = (TextView) findViewById(R.id.title_txt);
		titleTxt.setText(getString(R.string.sessions_txt));
		addButton = (ImageButton) findViewById(R.id.title_action_image_btn);
		addButton.setVisibility(View.VISIBLE);
	
	}

	private void loadBundleData(Bundle bundle) {
		sessionName = bundle.getString(SESSION_SESSION_NAME);
		host = bundle.getString(SESSION_SESSION_HOST);
		port = bundle.getString(SESSION_SESSION_PORT);
		username = bundle.getString(SESSION_SESSION_USERNAME);
		password = bundle.getString(SESSION_SESSION_PASSWORD);
	}
	
	private void initUI(){
		nameTxt = (EditText)findViewById(R.id.add_session_name_txt);
		nameTxt.setText(sessionName);
		nameTxt.setEnabled(false);
		hostTxt = (EditText)findViewById(R.id.add_session_host_txt);
		hostTxt.setText(host);
		hostTxt.setEnabled(false);
		portTxt = (EditText)findViewById(R.id.add_session_port_txt);
		portTxt.setText(port);
		portTxt.setEnabled(false);
		usernameTxt = (EditText)findViewById(R.id.add_session_username_txt);
		usernameTxt.setText(username);
		usernameTxt.setEnabled(false);
		passwordTxt = (EditText)findViewById(R.id.add_session_password_txt);
		passwordTxt.setText(password);
		passwordTxt.setEnabled(false);
		deleteSessionBtn = (Button)findViewById(R.id.add_session_add_edit_delete_btn);
		deleteSessionBtn.setText(getResources().getString(R.string.delete_session_txt));
	}
	
	private void setListeners() {
		deleteSessionBtn.setOnClickListener(this);
	}

	private void initDb() {
		db = new DbService(this);
	}

	@Override
	public void onClick(View v) {
		processData();
	}

	private void processData() {
		if(isDataValid(sessionName,host,port,username,password)) {
			Session session = new Session(sessionName, host, port, username, password);
			deleteSession(session);
			DeleteSessionActivity.this.finish();
		}
	}
	
	private void deleteSession(Session session) {
		db.Delete(session);
	}
	
	private boolean isDataValid(String name, String host, String port,
			String username, String password) {
	
		return true;
	}
	
}